//
//  ViewRecordAndMergeDemoViewController.swift
//  Persistance
//
//  Created by apple on 2018/10/29.
//  Copyright © 2018 apple. All rights reserved.
//

import UIKit

class ViewRecordAndMergeDemoViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.groupTableViewBackground
        view.addSubview(itemView)
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        self.itemView.frame = self.view.bounds
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        print("update item to data center\n")
        self.itemView.name = "car"
        self.itemView.detail = "this is a vary fast car"
        self.dataCenter.updateItemToDataCenter(itemView: self.itemView)
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            
            self.dataCenter.updateItemToDataCenter(itemView: self.itemView)
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
            
            self.dataCenter.deleteItem(itemView: self.itemView)
        }
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
            
            self.itemView.name = "hello, world!"
            self.itemView.detail = "I am a new item now!"
            self.dataCenter.insert(itemView: self.itemView)
        }
    }
    
    //MARK: getter and setter
    private lazy var itemView: ItemView = {
        
        return dataCenter.createNewItem()
    }()
    
    private lazy var dataCenter: ItemDataCenter = {
        return ItemDataCenter()
    }()
    
}
